package light.mvc.controller.doctor;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.page.base.Grid;
import light.mvc.page.base.Json;
import light.mvc.page.base.PageFilter;
import light.mvc.page.base.SessionInfo;
import light.mvc.page.base.Status;
import light.mvc.page.doctor.ChargeXRay;
import light.mvc.page.doctor.RegisterCharge;
import light.mvc.page.patient.Register;
import light.mvc.service.doctor.ChargeXRayServiceI;
import light.mvc.service.patient.RegisterSeqServiceI;
import light.mvc.utils.FileUploadingUtil;
import light.mvc.utils.GlobalConstant;
import light.mvc.utils.TimeUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/chargeXRay")
public class ChargeXRayController extends BaseController {
	@Autowired
	private ChargeXRayServiceI chargeXRayService;
	
	@Autowired
	private RegisterSeqServiceI registerSeqService;
	
	@RequestMapping("/manager")
	public String manager(HttpServletRequest request) {
		Date timeSegment = null;
		try {
			timeSegment = TimeUtil.getNowTimeSegment();
		} catch (Exception e) {
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("errorCode", "1");
			return "/doctor/chargeXRay";
		}
		request.setAttribute("msg", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timeSegment));
		request.setAttribute("errorCode", "0");
		return "/doctor/chargeXRay";
	}
	
	@RequestMapping("/patientGrid")
	@ResponseBody
	public Grid patientGrid(HttpServletRequest request, PageFilter ph,String timeSegmentStr) {
		SessionInfo sessionInfo = (SessionInfo)request.getSession().getAttribute(GlobalConstant.SESSION_INFO);
		
		Date timeSegment = TimeUtil.str2Date(timeSegmentStr);
		
		Grid grid = new Grid();
		Integer roleId = null;
		if(sessionInfo.getRoleId() == 4) {
			roleId = sessionInfo.getId();
		}
		List<Register> registers = registerSeqService.getPatientsByDoctorId(roleId, timeSegment);
		grid.setRows(registers);
		grid.setTotal((long)registers.size());
		return grid;
	}
	
	@RequestMapping("/detailPage")
	public String detailPage(HttpServletRequest request,Integer registerId) {
		List<RegisterCharge> registerCharges = chargeXRayService.getRegisterCharge(registerId);
		request.setAttribute("registerCharges", registerCharges);
		request.setAttribute("registerId", registerId);
		return "/doctor/chargeXRayDetail";
	}
	
	@RequestMapping("/historyPage")
	public String historyPage(HttpServletRequest request,Integer patientId,Date timeSegment) {
		List<RegisterCharge> registerCharges = chargeXRayService.getRegisterChargeByPatientId(patientId,timeSegment);
		request.setAttribute("registerCharges", registerCharges);
		return "/doctor/chargeXRayHistory";
	}
	
	@RequestMapping("/addCharge")
	@ResponseBody
	public Json addCharge(Integer xRayId,Integer registerId) {
		Json j;
		String errorMsg = null;
		Integer errorCode = -1;
		
		try {
			chargeXRayService.addCharge(registerId, xRayId);
			errorCode = 0;
			errorMsg = "开单成功";
		} catch (Exception e) {
			errorMsg = e.getMessage();
		}
		
		j = new Json(new Status(errorCode, errorMsg),null);
		return j;
	}
	
	@RequestMapping("/charge")
	@ResponseBody
	public Json charge(Integer registerChargeId) {
		Json j;
		String errorMsg = null;
		Integer errorCode = -1;
		
		try {
			chargeXRayService.charge(registerChargeId);
			errorCode = 0;
			errorMsg = "缴费成功";
		} catch (Exception e) {
			errorMsg = e.getMessage();
		}
		
		j = new Json(new Status(errorCode, errorMsg),null);
		return j;
	}
	
	@RequestMapping("/addXRayPage")
	public String addXRayPage(HttpServletRequest request,Integer registerChargeId) {
		request.setAttribute("registerChargeId", registerChargeId);
		return "/doctor/chargeXRayAdd";
	}
	
	@RequestMapping("/xRay")
	@ResponseBody
	public Json xRay(Integer registerChargeId,HttpServletRequest request) {
		Json j;
		String errorMsg = null;
		Integer errorCode = -1;
		
		//接受图片，返回图片地址
		Map<String, String> fileUrls = null;
		try {
			fileUrls = FileUploadingUtil.getUploaded(request);
		} catch (Exception e) {
			errorMsg = "上传失败";
			j = new Json(new Status(errorCode, errorMsg),null);
		}
		
		ChargeXRay chargeXRay = new ChargeXRay();
		chargeXRay.setRegisterChargeId(registerChargeId);
		
		String imageSrc = fileUrls.get("xRay"+ registerChargeId);
		imageSrc = imageSrc.substring(imageSrc.indexOf(GlobalConstant.uploadFolder));
		chargeXRay.setImageSrc(imageSrc);
		
		try {
			chargeXRayService.addXRay(chargeXRay);
			errorMsg = "放射成功";
			errorCode = 0;
			j = new Json(new Status(errorCode, errorMsg),null);
		} catch (Exception e) {
			errorMsg = "放射失败:" + e.getMessage();
			j = new Json(new Status(errorCode, errorMsg),null);
		}
		
		return j;
		
	}

}
